/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.RepeatFaultPlanListDTO;
import org.springblade.modules.admin.vo.ElevatorVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.ElevatorRepeatFaultLog;
import org.springblade.modules.admin.vo.ElevatorRepeatFaultLogVO;
import org.springblade.modules.admin.wrapper.ElevatorRepeatFaultLogWrapper;
import org.springblade.modules.admin.service.IElevatorRepeatFaultLogService;
import org.springblade.core.boot.ctrl.BladeController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 人员-大屏配置 控制器
 *
 * @author ysq
 * @since 2024-09-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/elevator/repeat/fault/log")
@Api(value = "人员-大屏配置", tags = "人员-大屏配置接口")
public class ElevatorRepeatFaultLogController extends BladeController {

	private final IElevatorRepeatFaultLogService elevatorRepeatFaultLogService;

	/**
	 * 详情
	 */
	@TenantIgnore
	@GetMapping("/plan/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "工单事件列表", notes = "传入事件等级level" +
		"工单状态 planStatus 2，3" +
		"事件范围 startDate endDate" +
		"是否超时 isOvertime 是 否" +
		"是否有指定角色 isRole 是 否")
	public R<List<RepeatFaultPlanListDTO>> detail(String level,String planStatus,Date startDate,
												  Date endDate,String isOvertime,String isRole,
												  String elevatorAddress,String elevatorDeptNames) {
		return R.data(elevatorRepeatFaultLogService.selectElevatorRepeatFaultPlanList(level,planStatus,startDate
			,endDate,isOvertime,isRole,elevatorAddress,elevatorDeptNames));
	}

	/**
	 * 详情
	 */
	@TenantIgnore
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入elevatorRepeatFaultLog")
	public R<ElevatorRepeatFaultLog> detail(Long id) {
		ElevatorRepeatFaultLog detail = elevatorRepeatFaultLogService.selectDetail(id);
		return R.data(detail);
	}

	@TenantIgnore
	@ApiOperation("下载明细")
	@PostMapping("down/excel/detail")
	public void downExcelDetail(HttpServletResponse response, @RequestBody ElevatorRepeatFaultLog repeatFaultLog) throws IOException {
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("项目名称", "电梯位置","重复故障标签", "重复故障统计标签","最近报障时间", "最近工单执行人", "最近工单状态");
			rows.add(title);
			elevatorRepeatFaultLogService.downExcelDetail(repeatFaultLog.getId(), rows);
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			CommonUtil.setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}

	}

//	@TenantIgnore
//	@ApiOperation("下载明细")
//	@PostMapping("down/excel/plan")
//	public void downExcelPlan(HttpServletResponse response,Integer level,Integer planStatus,Date startDate,Date endDate,String isOvertime,String isRole) throws IOException {
//		ExcelWriter excelWriter = null;
//		ServletOutputStream outputStream = response.getOutputStream();
//		try {
//			List<List<String>> rows = new ArrayList<>();
//			List<String> title = CollUtil.newArrayList
//				("故障风险等级", "故障风险标签","电梯编号", "电梯名称","项目名称", "所属维保组", "风险关闭时长要求"
//				,"处理建议","风险处理进展","风险预警时间","风险关闭时间","风险关闭是否超时","风险处理人","是否调度技术支持/专家"
//				,"技术支持/专家姓名","风险关连工单ID");
//			rows.add(title);
//			List<RepeatFaultPlanListDTO> resultList = elevatorRepeatFaultLogService.selectElevatorRepeatFaultPlanList(level,planStatus,startDate,endDate,isOvertime,isRole);
//			if(Func.isNotEmpty(resultList)){
//				resultList.forEach(e -> {
//					List<String> row = new ArrayList<>(7);
//
//				});
//			}
//			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
//			CommonUtil.setResponseHeader(response, fileName);
//			excelWriter = ExcelUtil.getBigWriter(rows.size());
//			excelWriter.write(rows, true);
//			excelWriter.flush(outputStream, true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (excelWriter != null) {
//				excelWriter.close();
//			}
//			outputStream.close();
//		}
//
//	}

}
